package com.dkt.api.web;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dkt.api.service.ItemSrv;
import com.dkt.api.web.dto.Dto;
import com.dkt.api.web.dto.ItemDto;
import com.dkt.base.NetConsts;
import com.dkt.base.Resps;
import com.dkt.base.ValidUtil;
import com.dkt.base.domain.Item;

/**
 *
 * @author penghy
 * @version 1.0.0
 *
 */
@RestController
@RequestMapping(value = "/item")
public class ItemCtrl {

	@Autowired
	private ItemSrv itemSrv;

	/**
	 * 检索商品
	 *
	 * @return
	 * @version 2.0
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Object list(@Validated(Dto.Srch.class) ItemDto item, Errors errors, HttpServletResponse response)
			throws Exception {
		if (errors.hasErrors()) {
			return ValidUtil.errResps(response, errors);
		}

		List<Item> items = itemSrv.itemList(item);
		Resps<List<Item>> resps = new Resps<>(NetConsts.RC_SUCCESS, new String[] {"list success"});
		resps.setData(items);
		return resps;
	}
	
	/**
	 * 添加商品
	 *
	 * @return
	 * @version 2.0
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Object add(@Validated(Dto.Add.class) ItemDto itemDto, Errors errors, HttpServletResponse response)
			throws Exception {
		if (errors.hasErrors()) {
			return ValidUtil.errResps(response, errors);
		}

		itemSrv.addItem(itemDto);
		return new Resps<>(NetConsts.RC_SUCCESS, null);
	}
	
	
	/**
	 * 删除商品
	 *
	 * @return
	 * @version 2.0
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Object del(Integer id, Errors errors, HttpServletResponse response)
			throws Exception {
		if (errors.hasErrors()) {
			return ValidUtil.errResps(response, errors);
		}
		
		itemSrv.delByItem(id);
		return new Resps<>(NetConsts.RC_SUCCESS, null);
	}
}